import { AddPostionParam, EditPositionParam, GetPositionParam, PositionDetailParam, PostionMemberParam, UserListParam } from "@Tea/model"
import express, { Request, Response } from "express"
import { PositionManageContext } from "../../../../context/PositionManageContext"
import { checkError } from "../../../../utils/token"
import { useCheckAndResponse } from "../../../../utils/common"
import { UserMangeContext } from "../../../../context/UserMangeContext"

const positionManage = express.Router()

positionManage.post("/positonList", async (req: Request, res: Response) => {
  const params = req.body as GetPositionParam
  try {
    const data = await PositionManageContext.getInstance().getPositionList(params)
  
    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg:checkError(ex)
    })
  }
})

positionManage.post("/positonDetail", async (req: Request, res: Response) => {
  const params = req.body as PositionDetailParam
  try {
    const data = await PositionManageContext.getInstance().getPositionDetail(params)
  
    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        ...data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg:checkError(ex)
    })
  }
})

positionManage.get("/addableList",async (req: Request, res: Response) => {
  try {
    res.send({
      code: 1,
      msg: "查询成功",
      ...(await UserMangeContext.getInstance().getUserList({ username: "", enable: 1 ,currentPage:1,pageSize:1000}, "admin"))
    })
  } catch (e) {
    res.status(500).send({
      code: 0,
      msg: checkError(e)
    })
  }
})

positionManage.post("/editPosition",async (req: Request, res: Response) => {
  try {
    const params = req.body as EditPositionParam
    const result = await PositionManageContext.getInstance().editPosition(params)
    
    useCheckAndResponse(res, result[0], '编辑成功')
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

positionManage.post("/addPosition", async (req: Request, res: Response) => {
  try {
    const params = req.body as AddPostionParam
    const result = await PositionManageContext.getInstance().addPosition(params)
    
    useCheckAndResponse(res, result[0], '新增成功')
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

positionManage.delete("/delPosition", async (req: Request, res: Response) => {
  try {
    const positionCode = (req.query as { positionCode: string }).positionCode
    const result = await PositionManageContext.getInstance().delPosition(positionCode)
    
    useCheckAndResponse(res, result[0], "删除成功")
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

positionManage.post("/addMember", async (req: Request, res: Response) => {
  try {
    const result = await PositionManageContext.getInstance().addMember(req.body as PostionMemberParam)
    
    useCheckAndResponse(res, result[0], "添加成功")
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

positionManage.delete("/delMember", async (req: Request, res: Response) => {
  try {
    const id = req.query.id as unknown  as number
    const result = await PositionManageContext.getInstance().delMember(id)
    
    useCheckAndResponse(res, result[0], "删除成功")
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

export default positionManage